﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Services;
using DCS.ComLib.Config;
using VietThuongLMS.Utils;
using VietThuongLMS.Areas.Admin.Models;
using System.IO;

///Author: NHDuc
///Created Date: 25/12/2013
namespace VietThuongLMS.Areas.Admin.Controllers
{
    public class AlbumController : Controller
    {
        //
        // GET: /Admin/Album/

        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Index()
        {
            if (!Request.IsAuthenticated)
                return RedirectToAction("Login", "Home", new { area = "Admin" });

            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            AlbumViewModel AlbumList = AlbumService.GetAlbumAllList(int.Parse(ConfigReader.ReadString("ItemPerPage")), page);

            //phân trang
            Pagination pagination = new Pagination(true, Url.Content("~"), "Admin", "Album", "Index", "?p=", AlbumList.Total, page, int.Parse(ConfigReader.ReadString("ItemPerPage")), "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(AlbumList);
        }

        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Detail(int id)
        {
            Album Album = AlbumService.GetAlbumById(id);
            return View(Album);
        }

        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Create()
        {
            return View();
        }

        [Authorize(Roles = "Admin,NhanVien")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Create(AlbumModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Album data = new Album();
                    data.TenAlbum = model.TenAlbum;
                    data.Tag = model.Tag;
                    data.Type = model.Type;

                    //url
                    if (model.Url != null && !model.Url.FileName.Equals(""))
                    {
                        string fileName = Path.GetFileName(model.Url.FileName);
                        DCS.ComLib.File.Log.Write("AlbumController::Create", "", this.Server.MapPath("~/Images/HoatDong/"));
                        try
                        {
                            fileName = Utils.Upload.UpdateImageWithSize(model.Url, 320, this.Server.MapPath("~/Images/HoatDong/"));
                        }
                        catch (Exception ex)
                        {
                            DCS.ComLib.File.Log.Write("AlbumController::Create", ex.StackTrace, ex.Message);
                        }
                        data.Url = "Images/HoatDong/" + fileName;
                    }

                    bool result = AlbumService.InsertAlbum(data);
                    if (result)
                        return RedirectToAction("Index", "Album", new { area = "Admin" });
                    return View();
                }
                catch (Exception ex)
                {

                }
            }
            return View(model);
        }

        //delete Account
        [Authorize(Roles = "Admin,NhanVien")]
        [HttpGet]
        public ActionResult Delete(int id)
        {
            bool result = AlbumService.DeleteAlbum(id);
            if (result)
            {
                return RedirectToAction("Index", "Album", new { area = "Admin" });
            }
            else
            {
                return View();
            }

        }

        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Edit(int id)
        {
            Album Album = AlbumService.GetAlbumById(id);
            AlbumModel model = (new AlbumConverter()).AlbumToModel(Album);
            ViewData["avatar"] = model.strUrl;
            return View(model);
        }

        [Authorize(Roles = "Admin,NhanVien")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(AlbumModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Album data = new Album();
                    data.ID = model.ID;
                    data.TenAlbum = model.TenAlbum;
                    data.Type = model.Type;
                    data.Tag = model.Tag;
                    //url
                    if (model.Url != null && !model.Url.FileName.Equals(""))
                    {
                        if (model.Url.ContentLength > 1048576)
                        {
                        }
                        else
                        {
                            string fileName = Path.GetFileName(model.Url.FileName);
                            DCS.ComLib.File.Log.Write("AlbumController::Edit", "", this.Server.MapPath("~/Images/HoatDong/"));
                            try
                            {
                                fileName = Utils.Upload.UpdateImageWithSize(model.Url, 320, this.Server.MapPath("~/Images/HoatDong/"));
                            }
                            catch (Exception ex)
                            {
                                DCS.ComLib.File.Log.Write("AlbumController::Edit", ex.StackTrace, ex.Message);
                            }
                            data.Url = "Images/HoatDong/" + fileName;
                        }
                    }

                    bool result = AlbumService.Edit(data);
                    if (result)
                        return RedirectToAction("Index", "Album", new { area = "Admin" });
                    Album Album = AlbumService.GetAlbumById(model.ID);
                    AlbumModel model1 = (new AlbumConverter()).AlbumToModel(Album);
                    return View(model1);
                }
                catch (Exception ex)
                {

                }
            }
            return View(model);
        }

    }
}
